package cn.qu.netty.boot;

import java.net.InetSocketAddress;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.HttpMessage;

public class CheckIpInboundHandler extends ChannelInboundHandlerAdapter {

	final static Log log = Log.createLog();

	public static final String CHECK_IP_HEADER = "CHECK_IP_HEADER";

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		if (msg instanceof HttpMessage) {
			HttpMessage req = (HttpMessage) msg;
			InetSocketAddress remoteAddress = (InetSocketAddress) ctx.channel().remoteAddress();
			req.headers().set(CHECK_IP_HEADER, remoteAddress.getHostName());
			ctx.fireChannelRead(msg);
		}
	}

}
